package com.inuker.bluetooth;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hjq.permissions.OnPermissionCallback;
import com.inuker.bluetooth.library.connect.listener.BluetoothStateListener;
import com.inuker.bluetooth.library.search.SearchRequest;
import com.inuker.bluetooth.library.search.SearchResult;
import com.inuker.bluetooth.library.search.response.SearchResponse;
import com.inuker.bluetooth.library.utils.BluetoothLog;
import com.inuker.bluetooth.library.utils.BluetoothUtils;
import com.inuker.bluetooth.view.PullRefreshListView;
import com.inuker.bluetooth.view.PullToRefreshFrameLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

public class MainActivity extends Activity {
    
    private static final String MAC = "B0:D5:9D:6F:E7:A5";
    
    private PullToRefreshFrameLayout mRefreshLayout;
    private PullRefreshListView mListView;
    private DeviceListAdapter mAdapter;
    private TextView mTvTitle;
    
    private List<SearchResult> mDevices;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mDevices = new ArrayList<SearchResult>();
        
        mTvTitle = (TextView) findViewById(R.id.title);
        
        mRefreshLayout = (PullToRefreshFrameLayout) findViewById(R.id.pulllayout);
        
        mListView = mRefreshLayout.getPullToRefreshListView();
        mAdapter = new DeviceListAdapter(this);
        mListView.setAdapter(mAdapter);
        
        mListView.setOnRefreshListener(new PullRefreshListView.OnRefreshListener() {
            
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                searchDevice();
            }
            
        });
        
        
        ClientManager.getClient().registerBluetoothStateListener(new BluetoothStateListener() {
            @Override
            public void onBluetoothStateChanged(boolean openOrClosed) {
                BluetoothLog.v(String.format("onBluetoothStateChanged %b", openOrClosed));
            }
        });
        
        BluetoothUtils.requestPermission(this, new OnPermissionCallback() {
            @Override
            public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                if (allGranted) {
                    if (BluetoothUtils.isGpsEnable(MainActivity.this)) {
                        searchDevice();
                    } else {
                        BluetoothUtils.openGpsSetting(MainActivity.this, 0x04);
                    }
                }
            }
            
            @Override
            public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                OnPermissionCallback.super.onDenied(permissions, doNotAskAgain);
            }
        });
        
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x04) {
            if (BluetoothUtils.isGpsEnable(MainActivity.this)) {
                searchDevice();
            }
        }
    }
    
    private void searchDevice() {
        SearchRequest request = new SearchRequest.Builder()
            .searchBluetoothLeDevice(5000, 2).build();
        
        ClientManager.getClient().search(request, mSearchResponse);
    }
    
    private final SearchResponse mSearchResponse = new SearchResponse() {
        @Override
        public void onSearchStarted() {
            BluetoothLog.w("MainActivity.onSearchStarted");
            mListView.onRefreshComplete(true);
            mRefreshLayout.showState(AppConstants.LIST);
            mTvTitle.setText(R.string.string_refreshing);
            mDevices.clear();
        }
        
        @Override
        public void onDeviceFounded(SearchResult device) {
            //            BluetoothLog.w("MainActivity.onDeviceFounded " + device.device.getAddress());
            if (!mDevices.contains(device) && device.getName() != null && !device.getName().equals("NULL")) {
                mDevices.add(device);
                mAdapter.setDataList(mDevices);
                
                //                Beacon beacon = new Beacon(device.scanRecord);
                //                BluetoothLog.v(String.format("beacon for %s\n%s", device.getAddress(), beacon.toString()));
                
                //                BeaconItem beaconItem = null;
                //                BeaconParser beaconParser = new BeaconParser(beaconItem);
                //                int firstByte = beaconParser.readByte(); // 读取第1个字节
                //                int secondByte = beaconParser.readByte(); // 读取第2个字节
                //                int productId = beaconParser.readShort(); // 读取第3,4个字节
                //                boolean bit1 = beaconParser.getBit(firstByte, 0); // 获取第1字节的第1bit
                //                boolean bit2 = beaconParser.getBit(firstByte, 1); // 获取第1字节的第2bit
                //                beaconParser.setPosition(0); // 将读取起点设置到第1字节处
            }
            
            if (mDevices.size() > 0) {
                mRefreshLayout.showState(AppConstants.LIST);
            }
        }
        
        @Override
        public void onSearchStopped() {
            BluetoothLog.w("MainActivity.onSearchStopped");
            mListView.onRefreshComplete(true);
            mRefreshLayout.showState(AppConstants.LIST);
            
            mTvTitle.setText(R.string.devices);
        }
        
        @Override
        public void onSearchCanceled() {
            BluetoothLog.w("MainActivity.onSearchCanceled");
            
            mListView.onRefreshComplete(true);
            mRefreshLayout.showState(AppConstants.LIST);
            
            mTvTitle.setText(R.string.devices);
        }
    };
    
    @Override
    protected void onPause() {
        super.onPause();
        ClientManager.getClient().stopSearch();
    }
}
